package com.topvision.newprlibrary;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.guo.android_extend.image.ImageConverter;

/**
 * @author lucy
 * @date 2018-11-28 16:35
 * @description //TODO
 */

public class ImageUtil {
    public static int[] getImageWidthHeight(String path){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        return new int[]{options.outWidth,options.outHeight};
    }

    public ImageConverter convert;

    public static byte[] convertNv21(Bitmap mBitmap) {
        ImageConverter convert = new ImageConverter();
        //TODO 有待优化地方
        byte[] data = new byte[mBitmap.getWidth() * mBitmap.getHeight() * 3 / 2];
        try {
            convert.initial(mBitmap.getWidth(), mBitmap.getHeight(), ImageConverter.CP_PAF_NV21);
            if (!convert.convert(mBitmap, data)) {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        convert.destroy();
        return data;
    }

    public static byte[] convertNv21(String filePath) {
        ImageConverter convert = new ImageConverter();
        Bitmap bitmap = BitmapFactory.decodeFile(filePath);
        byte[] data = new byte[bitmap.getWidth() * bitmap.getHeight() * 3 / 2];
        try {
            convert.initial(bitmap.getWidth(), bitmap.getHeight(), ImageConverter.CP_PAF_NV21);
            if (!convert.convert(bitmap, data)) {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        convert.destroy();
        return data;
    }
}
